package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.markdownconverter.lib.MdConvertUtils;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityMdConvertBinding;
import flc.ast.dialog.FileNameDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.WorkPathUtil;

public class MdConvertActivity extends BaseAc<ActivityMdConvertBinding> {

    public static int type;
    public static String sPath;
    private String savePath;

    @Override
    protected int onCreate() {
        return R.layout.activity_md_convert;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMdConvertBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                showFileNameDialog();
                break;
        }
    }

    @Override
    protected void initData() {
        String suffix;
        if (type == 0) {
            suffix = ".docx";
        } else {
            suffix = ".pdf";
        }
        savePath = PathUtils.getFilesPathExternalFirst() + Content.myTemp + "/" + FileUtils.getFileNameNoExtension(sPath) + suffix;
        FileUtils.createOrExistsFile(savePath);
        mDataBinding.tvFileName.setText(FileUtils.getFileName(sPath));
        mDataBinding.tvFileSize.setText(getString(R.string.all_text) + ConvertUtils.byte2FitMemorySize(FileUtils.getLength(sPath), 0));
        if (type == 0) {
            mDataBinding.ivConvertImg.setImageResource(R.drawable.khcfdc_word);
            mdConvertWord();
        } else {
            mDataBinding.ivConvertImg.setImageResource(R.drawable.apdf4);
            mdConvertPdf();
        }
    }

    private void mdConvertWord() {
        Random random = new Random();
        mDataBinding.sbProgress.setProgress(random.nextInt(50) + 10);
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                try {
                    MdConvertUtils.convertToWord(sPath, savePath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                emitter.onNext(savePath);
            }

            @Override
            public void accept(String path) {
                mDataBinding.sbProgress.setProgress(100);
                mDataBinding.tvConvertTip.setTextColor(Color.parseColor("#19A910"));
                mDataBinding.tvConvertTip.setText(getString(R.string.file_convert_suc));
                mDataBinding.ivConvertSuc.setVisibility(View.VISIBLE);
                mDataBinding.tvSave.setVisibility(View.VISIBLE);
            }
        });
    }


    private void mdConvertPdf() {
        Random random = new Random();
        mDataBinding.sbProgress.setProgress(random.nextInt(50) + 10);
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                try {
                    MdConvertUtils.convertToPdf(sPath, savePath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                emitter.onNext(savePath);
            }

            @Override
            public void accept(String path) {
                mDataBinding.sbProgress.setProgress(100);
                mDataBinding.tvConvertTip.setTextColor(Color.parseColor("#19A910"));
                mDataBinding.tvConvertTip.setText(getString(R.string.file_convert_suc));
                mDataBinding.ivConvertSuc.setVisibility(View.VISIBLE);
                mDataBinding.tvSave.setVisibility(View.VISIBLE);
            }
        });
    }

    private void showFileNameDialog() {
        FileNameDialog dialog = new FileNameDialog(mContext);

        dialog.name = FileUtils.getFileNameNoExtension(sPath);

        dialog.setListener(new FileNameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                renameFile(name);
            }
        });

        dialog.show();
    }

    //重命名文件
    private void renameFile(String newName) {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String suffix = FileUtils.getFileExtension(savePath);
                String desPath = PathUtils.getFilesPathExternalFirst() + Content.myFile + "/" + newName + "." + suffix;
                FileUtils.copy(savePath, desPath);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.save_success));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setResult(RESULT_OK);
                        finish();
                    }
                }, 500);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + Content.myTemp);
    }
}